$(document).ready(function () {
  //拿到所有实体的配置
  $.get(
    "http://localhost:8013/base/config/object/getStandardConfig",
    {},
    function (data) {
      if (data.code === 200) {
        $("#main_sidebar").empty();

        var uls = $('<ul class="side-nav"></ul>');
        uls.appendTo("#main_sidebar");

        var newData = _.forEach(_.get(data, "data", []), item => {
          item.href = "/standard/" + item.apiKey;
        });

        var session = [
          {
            id: "baseEntity",
            title: "标准实体",
            icon: "uil-home-alt",
            data: newData,
          },
          {
            id: "userEntity",
            title: "自定义实体",
            icon: "uil-home-alt",
            data: [
              {
                name: "自定义实体管理",
                _id: "-100010",
                href: "/otherEntity",
              },
            ],
          },
          {
            id: "allEntity",
            title: "所有实体",
            icon: "uil-home-alt",
            data: [
              {
                name: "查看所有实体信息",
                _id: "-100011",
                href: "/allEntity",
              },
            ],
          },
        ];

        session.forEach((sessionItem) => {
          var sessionView = $(
            '<li class="side-nav-item">' +
              '<a data-bs-toggle="collapse" href="#' +
              sessionItem.id +
              '" aria-expanded="false" aria-controls="sidebarDashboards" class="side-nav-link collapsed">' +
              '<i class="' +
              sessionItem.icon +
              '"></i>' +
              '<span class="badge bg-success float-end">' +
              sessionItem.data.length +
              "</span>" +
              "<span> " +
              sessionItem.title +
              " </span>" +
              "</a>" +
              "</li>"
          );
          sessionView.appendTo(uls);
          var secDiv = $(
            '<div class="collapse show" id="' +
              sessionItem.id +
              '" style=""></div>'
          );
          secDiv.appendTo(sessionView);
          var sec = $('<ul class="side-nav-second-level"></ul>');
          sec.appendTo(secDiv);

          _.forEach(sessionItem.data, (item) => {
            var liBody = $(
              '<li><a href="' + item.href + '">' + item.name + "</a></li>"
            );
            liBody.attr("_id", item._id);
            liBody.appendTo(sec);
          });
        });
      } else {
        showToast(response.msg, 3000);
      }
    }
  );
});
